package com.baizhi.serviceimpl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baizhi.annotation.AddLog;
import com.baizhi.dao.UserMapper;

import com.baizhi.entity.User;
import com.baizhi.entity.UserExample;
import com.baizhi.service.UserService;
import com.baizhi.util.AliyunOSSUtil;
import com.baizhi.util.UUIDUtil;
import com.baizhi.vo.CommonQueryPageVo;
import com.baizhi.vo.CommonVo;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Resource
    UserMapper userMapper;

    @Override
    public HashMap<String, Object> queryAllPage(Integer page, Integer pageSize) {

        HashMap<String, Object> map = new HashMap<>();

        //设置当前页
        map.put("page",page);

        //设置查询条件对象
        UserExample example = new UserExample();

        //查询总条数
        int count = userMapper.selectCountByExample(example);

        //设置总条数
        map.put("total",count);

        //创建分页对象  参数：起始条数，数据数
        RowBounds bounds = new RowBounds((page-1)*pageSize,pageSize);

        //根据分页查询数据
        List<User> users = userMapper.selectByExampleAndRowBounds(example, bounds);

        //设置查询数据
        map.put("rows",users);


        return map;
    }

    @AddLog(value = "修改用户")
    @Override
    public HashMap<String, Object> update(User user) {
        HashMap<String, Object> map = new HashMap<>();

        try {
            userMapper.updateByPrimaryKeySelective(user);
            map.put("message","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败");
        }

        return map;
    }

    @AddLog(value = "删除用户")
    @Override
    public HashMap<String, Object> delete(User user) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            //删除文件

            //根据文件信息
            User users = userMapper.selectOne(user);

            //获取图片网络路径
            String headImg = users.getHeadImg();

            //字符串处理
            String imgPath = headImg.replace("https://yingx-2019.oss-cn-beijing.aliyuncs.com/", "");

            AliyunOSSUtil.deleteBucket("yingx-2019",imgPath);

            //删除数据
            userMapper.delete(user);

            map.put("message","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败");
        }


        return map;
    }

    @Override
    public CommonQueryPageVo queryAllPages(Integer page, Integer pageSize) {

        //设置查询条件对象
        UserExample example = new UserExample();

        //查询总条数
        int count = userMapper.selectCountByExample(example);

        //创建分页对象  参数：起始条数，数据数
        RowBounds bounds = new RowBounds((page-1)*pageSize,pageSize);

        //根据分页查询数据
        List<User> users = userMapper.selectByExampleAndRowBounds(example, bounds);

        return new CommonQueryPageVo(page,count,users);
    }

    @AddLog(value = "修改用户")
    @Override
    public CommonVo updates(User user) {

        try {
            userMapper.updateByPrimaryKeySelective(user);
            return CommonVo.success("修改成功！！！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonVo.faild("修改失败！！！");
        }

    }

    @Override
    public void add(User user) {
        log.info("接收添加的数据：{}",user);

        user.setId(UUIDUtil.getUUID());

        userMapper.insertSelective(user);

        log.info("接收添加id后的数据：{}",user);
    }

    @Override
    public String uploadHeadImgAliyun(MultipartFile headImg) {
        String filename = headImg.getOriginalFilename();

        String newName = new Date().getTime()+"-"+filename;

        String bucketName="yingx-2019";
        String fileName="eaImg/"+newName;

        String nath = "https://yingx-2019.oss-cn-beijing.aliyuncs.com/"+fileName;


        String message=null;

        try {

            AliyunOSSUtil.uploadfileBytes(bucketName,fileName,headImg);

            message=nath;
        } catch (Exception e) {
            e.printStackTrace();
            message=nath;
        }
        return message;
    }

    @Override
    public User queryById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }
}
